package com.yyfax.pay.third.model.to.fuioudeposit.xml;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.yyfax.framework.model.BasePO;
import com.yyfax.pay.common.util.XmlUtil;
import com.yyfax.pay.third.model.to.fuiou.ProtocolPlaceOrderRetryFuiouContent;

/**
 * 结果响应xml实体
 * 
 * @author buyi
 * @date 2017-09-02 16:35:20
 * @since v2.0.0
 */
@SuppressWarnings("serial")
@XStreamAlias("ap")
public class RootXMLRsp<O> extends BasePO {

	@XStreamAlias("plain")
	private O plain;

	@XStreamAlias("signature")
	private String sign;

	public O getPlain() {
		return plain;
	}

	public void setPlain(O plain) {
		this.plain = plain;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}

	public String toXml() {
		// QNameMap qmap = new QNameMap();
		// qmap.setDefaultPrefix("");
		// StaxDriver staxDriver = new StaxDriver(qmap);
		// XStream xstream = new XStream(staxDriver);
		// xstream.autodetectAnnotations(true);
		// xstream.ignoreUnknownElements();
		// xstream.alias("ap", RootXMLRsp.class);
		// try {
		// StringWriter strWriter = new StringWriter();
		// StaxWriter sw = new StaxWriter(staxDriver.getQnameMap(), staxDriver.getOutputFactory().createXMLStreamWriter(strWriter), false, true);
		// xstream.marshal(this, sw);
		// sw.close();
		// return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + strWriter.toString();
		// } catch (Exception e) {
		// e.printStackTrace();
		// }
		// return null;
		return XmlUtil.toXmlForXStream(this, "ap");
	}

	/**
	 * XML字符串转换成实体{@link ProtocolPlaceOrderRetryFuiouContent}
	 * 
	 * @author heizq
	 * @date 2017-4-12 10:53:28
	 * @since v1.0.0
	 * @param xml
	 * @return 返回{@link ProtocolPlaceOrderRetryFuiouContent}
	 */
	public static Object fromXml(String xml, Class<?> plain) {
		// XStream xstream = new XStream();
		//
		// xstream.ignoreUnknownElements();
		// xstream.autodetectAnnotations(true);
		// try {
		// xstream.addDefaultImplementation(plain, RootXMLRsp.class.getDeclaredField("plain").getType());
		// } catch (NoSuchFieldException e) {
		// e.printStackTrace();
		// }
		// xstream.alias("ap", RootXMLRsp.class);
		// xstream.processAnnotations(RootXMLRsp.class);
		// xstream.alias("plain", plain);
		// xstream.processAnnotations(plain);
		// return xstream.fromXML(xml);
		return XmlUtil.fromXMLForXStream(xml, "ap", RootXMLRsp.class, "plain", plain);
	}

	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws Exception {
		String s = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ap><plain><resp_code>5343</resp_code><resp_desc>手机号重复</resp_desc><mchnt_cd>0002900F0354237</mchnt_cd><mchnt_txn_ssn>RE0720010000000000000000000112</mchnt_txn_ssn></plain><signature>kxuNFn+7giLNC/csk/kJfWRSgXiDQ1GpwhWLR0t/nC3yjxb4Hpcm2BXFuuy7XmbLXWooYaBNAl+uP9iNC2tpyLkkNnxeydXC4bAn2MTo8f8hI9WMQpBtXl6wP6sZvAlX+2latunSuYzr1XqORX1Tt3LX4z607Xh+Cw9hdI9znSI=</signature></ap>\n";

		RootXMLRsp<BasePlain> xml = (RootXMLRsp<BasePlain>) RootXMLRsp.fromXml(s, BasePlain.class);
		System.out.println(xml.getPlain().toXml());

		String s1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ap><plain><resp_code>0000</resp_code><mchnt_cd>0002900F0354237</mchnt_cd><mchnt_txn_ssn>948873111046937</mchnt_txn_ssn><busi_tp>PWTX</busi_tp><total_number>1</total_number><results><result><ext_tp></ext_tp><txn_date>20170908</txn_date><txn_time>133804</txn_time><mchnt_ssn>EW0520010000000000000000000015</mchnt_ssn><txn_amt>100</txn_amt><fuiou_acct_no>8118110104618805</fuiou_acct_no><cust_no>18500000001</cust_no><artif_nm>狄光</artif_nm><remark></remark><txn_rsp_cd>0000</txn_rsp_cd><rsp_cd_desc>成功</rsp_cd_desc></result></results></plain><signature>WnPPilOBIKjtyshzH/6Zxsg3FdeOpvAVpu/Vaabw6EES/Po65hiGMQnlBj/Xrr/3HCqYor+zXDLg8vVDZHDyexnN+lBh4pw/+85NduCJD5QN1nfc43G/onLU/VFx1RiAngg7vrc3MVra23YPjzvbOY09jVT98gkP14cMUX/yDS4=</signature></ap>";

		RootXMLRsp<ChargeWithdrawPlain> xml1 = (RootXMLRsp<ChargeWithdrawPlain>) RootXMLRsp.fromXml(s1, ChargeWithdrawPlain.class);
		System.out.println(xml1.getPlain().toXml());
	}
}
